#include <gtk/gtk.h>
#include <glib.h>
#include "formfun.h"
#include <stdlib.h>

static gboolean delete_event (GtkWidget *widget,
			      GdkEvent  *event,
			      gpointer  data)
{
    gtk_main_quit ();
    return FALSE;
}

static void clear_form (GtkWidget *widget,
			gpointer  data)
{
   form *theForm = (form *) data;
   gtk_entry_set_text (GTK_ENTRY (theForm->name ), "");
   gtk_entry_set_text (GTK_ENTRY (theForm->address ), "");
   gtk_entry_set_text (GTK_ENTRY (theForm->city ), "");
   gtk_entry_set_text (GTK_ENTRY (theForm->state ), "");
   gtk_entry_set_text (GTK_ENTRY (theForm->zip ), "");

   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (theForm->gender.male), TRUE);

   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (theForm->interests.puppies), FALSE);
   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (theForm->interests.food), FALSE);
   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (theForm->interests.sports), FALSE);
   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (theForm->interests.movies), FALSE);

}

static void update_status (GtkWidget *widget,
			   gpointer  data)
{
    form_and_widget *f_t = (form_and_widget *) data;
    GtkWidget       *text = f_t->text;
    GtkTextBuffer   *textBuffer;
    form            *theForm = f_t->theForm;
    char            buf[256];
    int		    len;

    len = sprintf (buf, "%s\n"
	       	   "%s\n"
		   "%s, %s %s\n\n"
		   "%s is a %s that is interested in %s%s%s%s.",
		   (char *) gtk_entry_get_text (GTK_ENTRY (theForm->name)),
		   (char *) gtk_entry_get_text (GTK_ENTRY (theForm->address)),
		   (char *) gtk_entry_get_text (GTK_ENTRY (theForm->city)),
		   (char *) gtk_entry_get_text (GTK_ENTRY (theForm->state)),
		   (char *) gtk_entry_get_text (GTK_ENTRY (theForm->zip)),
		   (char *) gtk_entry_get_text (GTK_ENTRY (theForm->name)),
		   (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (theForm->gender.male))) ? "Male" : "Female",
		   (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (theForm->interests.puppies))) ? "puppies" : "",
		   (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (theForm->interests.food))) ? ", food" : "",
		   (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (theForm->interests.sports))) ? ", sports" : "",
		   (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (theForm->interests.movies))) ? ", movies" : "");

    textBuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text));
    gtk_text_buffer_set_text (textBuffer, (gchar *) buf, len);


}

static form *new_form (void)
{
    form *nform = (form *) malloc (sizeof(form));
    nform->name = gtk_entry_new ();
    nform->address = gtk_entry_new ();
    nform->city = gtk_entry_new ();
    nform->state = gtk_entry_new ();
    nform->zip = gtk_entry_new ();

    nform->gender.male = gtk_radio_button_new_with_label (NULL, "Male");
    nform->gender.group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (nform->gender.male));
    nform->gender.female = gtk_radio_button_new_with_label (nform->gender.group, "Female");

    nform->interests.puppies = gtk_check_button_new_with_label ("Puppies");
    nform->interests.food = gtk_check_button_new_with_label ("Food");
    nform->interests.sports = gtk_check_button_new_with_label ("Sports");
    nform->interests.movies = gtk_check_button_new_with_label ("Movies");

    gtk_entry_set_width_chars(GTK_ENTRY (nform->state), 2);
    gtk_entry_set_width_chars(GTK_ENTRY (nform->zip), 5);

    gtk_widget_show (nform->name);
    gtk_widget_show (nform->address);
    gtk_widget_show (nform->city);
    gtk_widget_show (nform->state);
    gtk_widget_show (nform->zip);
    gtk_widget_show (nform->gender.male);
    gtk_widget_show (nform->gender.female);
    gtk_widget_show (nform->interests.puppies);
    gtk_widget_show (nform->interests.food);
    gtk_widget_show (nform->interests.sports);
    gtk_widget_show (nform->interests.movies);

    return nform;
}


int main (int argc, char *argv[])
{
    
    GtkWidget *window,
              *status,
              *button,
              *table,
              *label,
              *text;

    form *theForm;

    form_and_widget multi_callback;

    gtk_init (&argc, &argv);
    
    //create a new form of widgets
    theForm = new_form();

    //setup the window
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "Form Fun");
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);
    g_signal_connect (G_OBJECT (window), "delete_event",
		      G_CALLBACK (delete_event), NULL);
    
    //setup the status box
    status = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (status), "Status");
    gtk_window_set_default_size (GTK_WINDOW (status), 500, 300);
    text = gtk_text_view_new ();
    gtk_text_view_set_editable (GTK_TEXT_VIEW (text), FALSE);
    gtk_widget_show (text);
    gtk_container_set_border_width (GTK_CONTAINER (status), 10);
    gtk_container_add (GTK_CONTAINER (status), text);


    table = gtk_table_new (16, 4, TRUE);
    gtk_container_add (GTK_CONTAINER (window), table);

    label = gtk_label_new ("Name");
    gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 2, 0, 1);
    gtk_widget_show (label);
    gtk_table_attach_defaults (GTK_TABLE (table), theForm->name, 0, 2, 1, 2);

    label = gtk_label_new ("Address");
    gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 2, 3, 4);
    gtk_widget_show (label);
    gtk_table_attach_defaults (GTK_TABLE (table), theForm->address, 0, 2, 4, 5);

    label = gtk_label_new ("City");
    gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 2, 6, 7);
    gtk_widget_show (label);
    gtk_table_attach_defaults (GTK_TABLE (table), theForm->city, 0, 2, 7, 8);

    label = gtk_label_new ("State");
    gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 2, 9, 10);
    gtk_widget_show (label);
    gtk_table_attach_defaults (GTK_TABLE (table), theForm->state, 0, 2, 10, 11);

    label = gtk_label_new ("Zip");
    gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 2, 12, 13);
    gtk_widget_show (label);
    gtk_table_attach_defaults (GTK_TABLE (table), theForm->zip, 0, 2, 13, 14);

    label = gtk_label_new ("Gender");
    gtk_table_attach_defaults (GTK_TABLE (table), label, 3, 4, 0, 1);
    gtk_widget_show (label);
    gtk_table_attach_defaults (GTK_TABLE (table), theForm->gender.male, 3, 4, 1, 2);
    gtk_table_attach_defaults (GTK_TABLE (table), theForm->gender.female, 3, 4, 2, 3);

    label = gtk_label_new ("Interests");
    gtk_table_attach_defaults (GTK_TABLE (table), label, 3, 4, 4, 5);
    gtk_widget_show (label);
    gtk_table_attach_defaults (GTK_TABLE (table), theForm->interests.puppies, 3, 4, 5, 6);
    gtk_table_attach_defaults (GTK_TABLE (table), theForm->interests.food, 3, 4, 6, 7);
    gtk_table_attach_defaults (GTK_TABLE (table), theForm->interests.sports, 3, 4, 7, 8);
    gtk_table_attach_defaults (GTK_TABLE (table), theForm->interests.movies, 3, 4, 8, 9);

    button = gtk_button_new_with_label ("Clear");
    g_signal_connect (G_OBJECT (button), "clicked",
		      G_CALLBACK (clear_form), (gpointer) theForm);
    gtk_table_attach_defaults (GTK_TABLE (table), button, 1, 2, 15, 16);
    gtk_widget_show (button);

    button = gtk_button_new_with_label ("Update");
    multi_callback.theForm = theForm;
    multi_callback.text = text;
    g_signal_connect (G_OBJECT (button), "clicked",
		      G_CALLBACK (update_status), (gpointer) &multi_callback);
    gtk_table_attach_defaults (GTK_TABLE (table), button, 2, 3, 15, 16);
    gtk_widget_show (button);

    
    gtk_widget_show (table);
    gtk_widget_show (status);
    gtk_widget_show (window);
    
    gtk_main ();
    return 0;
}
